using System;
using Microsoft.Xna.Framework;

namespace SpriteAnimations.Graphics.Animations
{
    class BezierSplineAnimation : SpriteAnimation
    {
        Vector2 m_Pos1, m_Pos2, m_Pos3;
        Vector2 m_Speed;
        float Time;
        
        //
        // Constructors
        //
        public BezierSplineAnimation()
        {
            m_Pos1 = new Vector2(-70f, 500f);
            m_Pos3 = new Vector2(700f, 500f);
            //calculate the middle point
            float distance = Vector2.Distance(m_Pos1, m_Pos3);
            m_Pos2 = new Vector2(m_Pos1.X + (distance / 2), 0f);
            m_Speed = new Vector2(0.005f, 0f);
            Time = 0f;
        }

        //
        // Virtual Methods
        //
        public override void Animate(GameTime gameTime, SpriteParameters animatedParameters)
        {
            animatedParameters.Position = Quad(m_Pos1, m_Pos2, m_Pos3, Time);
            Time += m_Speed.X;
        }

        private Vector2 Quad(Vector2 pos1, Vector2 pos2, Vector2 pos3, float t) //returns the position on a quadratic bezier curve
        {
            return (1 - t) * (1 - t) * pos1 + 2 * t * (1 - t) * pos2 + t * t * pos3;
        }
    }
}
